package com.travelsky.flash.util;

import java.io.BufferedReader;
import java.io.InputStreamReader;

/**
 * @Description CMD命令执行
 * @author syuan
 * @date 2015年5月26日  上午9:58:30
 * Company: TravelSky
 */
public class CmdExecUtil {
	
	/**
	 * @Description CMD命令自行
	 * @param command CMD命令
	 * @return
	 * @author syuan
	 * @date 2015年5月26日  上午10:39:57
	 */
	public static String execCmd(String command) {
		String[] cmd = new String[] { "cmd.exe", "/c", command };
        try {
            Process process = Runtime.getRuntime().exec(cmd);
            StringBuffer info = new StringBuffer();
            
            // cmd输出流
			info.append("--信息-------------------------------------------\n");
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
			String line = reader.readLine();
			while (null != line) {
				if(!"".equals(line)) {
					info.append(line).append("\n");
				}
				line = reader.readLine();
				
			}
            process.getOutputStream().close();
            
            // cmd错误信息输出流 
			info.append("--错误-------------------------------------------\n");
			reader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
			line = reader.readLine();
			while (null != line && !"\n".equals(line)) {
				if(!"".equals(line)) {
					info.append(line).append("\n");
				}
				line = reader.readLine();
				
			}
			process.getErrorStream().close();
			
            int exitValue = process.waitFor();
			info.append("--结果-------------------------------------------\n");
			info.append(exitValue).append("\n");
			
			return info.toString();
			
        } catch (Exception e) {
            e.printStackTrace();
        }	
        
        return null;
	}
	
	public static void main(String[] args) {
		System.out.println(execCmd("mvn -version"));
	}
}
